<script setup>
import { reactive, useTemplateRef, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { handleLogger } from '@renderer/util/common'

const show = defineModel('show', { type: Boolean, default: false })
const emits = defineEmits(['success'])

const props = defineProps({
  editItemData: {
    type: [null, Object],
    default: null
  }
})

watch(
  () => show.value,
  (n) => {
    if (n) {
      formState.remark = props.editItemData.dealer_remark
    }
  }
)
const formRules = {
  remark: [{ required: true, message: '请输入备注', trigger: 'blur' }]
}
const formState = reactive({
  remark: ''
})
const formRef = useTemplateRef('formRef')
async function handleConfirm() {
  formRef.value.validate()
  const res = await window.api.updateDealerRemark(formState.remark, props.editItemData.dealer_id)
  console.log(res)
  ElMessage.success('修改成功')
  handleLogger(`修改经销商${props.editItemData.dealer_name}备注：${formState.remark}`)
  emits('success')
  show.value = false
}
</script>

<template>
  <el-dialog v-model="show" width="500" title="备注" draggable>
    <el-form ref="formRef" :model="formState" :rules="formRules">
      <el-form-item prop="remark">
        <el-input
          v-model="formState.remark"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
